<?php

//ckeditor 上传处理
class Ckupload extends Controller {
	
	function Ckupload()
	{
		parent::Controller();	
	}
	
	function index()
	{
	  //解析查询字符?CKEditorFuncNum=2
	  parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
	  $fn = $CKEditorFuncNum;
	  
	  //图片上传
	  $config['upload_path'] = './data/upload_ck/';
	  $config['allowed_types'] = 'gif|jpg|jpeg|png';
	  //$config['max_size'] = '2048';//k
	  $config['max_width']  = '0';
	  $config['max_height']  = '0';
	  $config['file_name']  = "ck_".strtotime(date('Y-m-d H:i:s'));
	  $this->load->library('upload', $config);
	  if ( !$this->upload->do_upload("upload")) {
		$this->mkhtml($fn,"","");
	  } 
	  $upload_data = $this->upload->data();
	  $filename = gurl("data/upload_ck").'/'.$upload_data['file_name'];
	  //$this->mkhtml($fn,$filename,"");
	  $file_name = $config['file_name'];
	  
	  //图片缩略
	  /*$config = array();
	  $config['image_library'] = 'gd2';
	  $config['source_image'] = './data/upload_ck/'.$upload_data['file_name'];
	  $config['create_thumb'] = TRUE;
	  $config['maintain_ratio'] = TRUE;
	  $config2['master_dim'] = 'auto';
	  $config['width'] = 300;
	  $config['height'] = 300;
	  $this->load->library('image_lib', $config); 
	  $this->image_lib->resize();
	  @unlink($config['source_image']);
	  $thumb_filename = gurl("data/upload_ck").'/'.$file_name."_thumb".$upload_data['file_ext'];*/
	  
	  $params = array('src_file' => './data/upload_ck/'.$upload_data['file_name'], 'dst_file' => './data/upload_ck/'.$file_name."_thumb".$upload_data['file_ext']);
	  $this->load->library("imagecrop", $params);
	  $this->imagecrop->Crop(300,300,1);
	  $this->imagecrop->SaveImage();
	  $this->imagecrop->destory();
	  
	  $thumb_filename = gurl("data/upload_ck").'/'.$file_name."_thumb".$upload_data['file_ext'];
	  $this->mkhtml($fn,$thumb_filename,"");
	}
	
	//输出js调用
	function mkhtml($fn,$fileurl,$message)
	{
		$str='<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$fn.', \''.$fileurl.'\', \''.$message.'\');</script>';
		exit($str);
	}
	
}

/* End of file */